package org.example.socket.pojo;

import java.util.HashMap;

public class Response {

    private int statusCode;

    private String version;

    private HashMap<String, String> headers;

    private String body;

    // 根据传入的响应头和响应体，生成响应对象
    public Response(int statusCode, HashMap<String, String> headers, String body) {
        this.statusCode = statusCode;
        this.version = "HTTP/1.1";
        this.headers = headers;
        this.body = body;
    }

    public String toResponseString() {
        StringBuilder sb = new StringBuilder();
        sb.append(version).append(" ").append(statusCode).append("\r\n");
        for (String key : headers.keySet()) {
            sb.append(key).append(": ").append(headers.get(key)).append("\r\n");
        }
        sb.append("\r\n");
        sb.append(body);
        return sb.toString();
    }

    @Override
    public String toString() {
        return "Response{" +
                "statusCode=" + statusCode +
                ", version='" + version + '\'' +
                ", headers=" + headers +
                ", body='" + body + '\'' +
                '}';
    }

    public int getStatusCode() {
        return statusCode;
    }

    public String getVersion() {
        return version;
    }

    public HashMap<String, String> getHeaders() {
        return headers;
    }

    public String getBody() {
        return body;
    }

    public void setStatusCode(int statusCode) {
        this.statusCode = statusCode;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public void setHeaders(HashMap<String, String> headers) {
        this.headers = headers;
    }

    public void setBody(String body) {
        this.body = body;
    }
}
